//
//  MBBTNCell.m
//  tableview多按钮选择
//
//  Created by MB__Lee on 2018/5/17.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBBTNCell.h"

#import "MBCoCell.h"



@interface MBBTNCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *colletionView;

@property (weak, nonatomic) IBOutlet UILabel *topLabel;

@property(nonatomic,strong)NSIndexPath *selectedIndexPath;//记录当前选中的 indexpath

@end
static NSString *MBCoCell_Identi = @"MBCoCell";
@implementation MBBTNCell

- (void)awakeFromNib {
    [super awakeFromNib];

//    CGFloat itemWidth = (self.bounds.size.width -  3 * Margin) * 0.25;
//    CGFloat itemHeight = itemWidth * 3/8;
    
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(itemWidth, itemHeight);
    layout.minimumLineSpacing = Margin;
    layout.minimumInteritemSpacing = Margin;
    self.colletionView.collectionViewLayout = layout;
    
    self.colletionView.delegate = self;
    self.colletionView.dataSource = self;
    
    [self.colletionView registerNib:[UINib nibWithNibName:MBCoCell_Identi bundle:nil] forCellWithReuseIdentifier:MBCoCell_Identi];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetBTNSate) name:@"resetBTN" object:nil];
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return  1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.cellMut.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MBCoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MBCoCell_Identi forIndexPath:indexPath];
    cell.title = self.cellMut[indexPath.row];
    
    return  cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    _selectedIndexPath = indexPath;
    MBCoCell *cell = (MBCoCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell selectedCell];
    
    if (_block) {
        _block(indexPath.row);
    }
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    MBCoCell *cell = (MBCoCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell deselectedCell];
    
}

#pragma mark ---flowlayout
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return  UIEdgeInsetsMake(0, Margin, 0, Margin);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return  Margin;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return  Margin;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return  CGSizeMake(itemWidth, itemHeight);
}




#pragma mark --添加显示的文字
-(void)setCellMut:(NSMutableArray *)cellMut{
    _cellMut = cellMut;
    
    [self.colletionView reloadData];
}
-(void)setTopLabelName:(NSString *)topLabelName{
    _topLabelName = topLabelName;
    
    self.topLabel.text = topLabelName;
    [self.colletionView reloadData];
}

#pragma mark --通知--让按钮恢复没选中状态
-(void)resetBTNSate{
    [self collectionView:self.colletionView didDeselectItemAtIndexPath:_selectedIndexPath];
}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
